
#include <string.h>
//
// Created by LanK on 2025/1/1.
//
#include <stdio.h>
int main() {
    int stack[100000];
    int top = -1;
    int n,x;
    char ch[100];
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%s", ch);
        if(!strcmp(ch,"push")) {
            scanf("%d", &x);
            stack[++top] = x;
        }else if(!strcmp(ch,"pop")) {
            if(top!=-1) {
                stack[top--];
            }
        }else if(!strcmp(ch,"empty")) {
            if(top!=-1) printf("NO\n");
            else printf("YES\n");
        }else if(!strcmp(ch,"query")) {
            printf("%d\n",stack[top]);
        }
    }
}